package pl.pawluk.inz.utils;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: mandp
 * Date: 03.05.13
 * Time: 18:44
 * To change this template use File | Settings | File Templates.
 */
public enum AuthoritiesEnum {
    ROLE_USER("ROLE_USER"),
    ROLE_ADMIN("ROLE_ADMIN"),
    ROLE_INSTRUCTOR("ROLE_INSTRUCTOR");

    private final String rola;

    private AuthoritiesEnum(String rola) {
        this.rola = rola;
    }

    @Override
    public String toString() {
        return rola;
    }

    public static Map<String, String> getRoles() {
        Map<String, String> res = new HashMap<String, String>();
        for (AuthoritiesEnum e : values()) {
            res.put(e.toString(), e.toString());
        }
        return res;
    }

    public static AuthoritiesEnum parse(String rola) {
        for (AuthoritiesEnum e : values()) {
            if (e.rola.equals(rola)) {
                return e;
            }
        }
        return null;
    }

    public static Set<AuthoritiesEnum> parse(Collection<String> roles) {
        if(roles == null || roles.size() == 0) {
            return null;
        }
        Set<AuthoritiesEnum> res = new HashSet<AuthoritiesEnum>();
        for (AuthoritiesEnum e : values()) {
            if (roles.contains(e.rola)) {
                res.add(e);
            }
        }
        return res;
    }
}
